<?php
include('ac_i18n.php');
include('helper/property_reader__helper.php');

/**
* Language dictionary (i18n)
*
* @author b3ha (baloghbh@gmail.com)
*/
class cI18nProperty extends acI18n
{
    const CONFIG        = 'config.ini';

    protected $config   = NULL;

    public function __construct()
    {
    }

    /**
    * Initialize
    */
    public function init(array $config = NULL)
    {
        $this->config = $config ? : parse_ini_file(self::CONFIG, TRUE);

        $this->setLanguage($this->config['defaultLanguage']);
    }

    /**
    * Load a property file from a module
    *
    * @param string $property_path
    * @param string $module
    */
    public function load($property_name, $module = 'main')
    {
        // property path
        $p_path     = NULL;
        // property file postfix (_languagecode)
        $postfix    = "_{$this->language}";
  
        switch ($module)
        {
            case 'main':
            {
                $p_path =   $this->config['paths']['application'] .
                            $this->config['paths']['language'] .
                            $property_name . 
                            $postfix .
                            '.properties';
            } break;

            default:
            {
                $p_path =   $this->config['paths']['application'] .
                            $this->config['paths']['modules'] .
                            $module .
                            '/' .
                            $this->config['paths']['language'] .
                            $property_name . 
                            $postfix .
                            '.properties';
            } break;
        }

        if ( ! file_exists($p_path))
        {
            throw new Exception("< {$property_name} > property file is not found at < {$p_path} >!!!");
        }

        $this->properties[] = $p_path;
        $this->dictionary   = array_merge($this->dictionary, property_reader($p_path));
    }

    /**
    * Load a all property files from a module
    *
    * @param string $module
    */
    public function loadAll($module = 'main')
    {
        // dir path
        $dir_path   = NULL;
        // property file postfix (_languagecode)
        $postfix    = "_{$this->language}";

        switch ($module)
        {
            case 'main':
            {
                $dir_path = $this->config['paths']['application'] .
                            $this->config['paths']['language'];
            } break;

            default:
            {
                $dir_path = $this->config['paths']['application'] .
                            $this->config['paths']['modules'] .
                            $module .
                            '/' .
                            $this->config['paths']['language'];
            } break;
        }

        if ( ! file_exists($dir_path))
        {
            throw new Exception("< {$dir_path} > path is not exists!!!");
        }

        foreach (glob($dir_path . '*') as $property)
        {
            if ($postfix === '')
            {
                if (preg_match('/^.*_([a-z]{2}).properties$/i', basename($property))) continue;
            }
            else if  ( ! preg_match("/^.*{$postfix}.properties$/i", basename($property))) continue;

            $this->properties[]	= $property;
            $this->dictionary	= array_merge($this->dictionary, property_reader($property));
        }
    }
}

# EOF
